const dbutil = require('../../../libs/MyDB');

module.exports = {
    getFoodInfo: async function () {
        let foods = [];
        await dbutil.multiSelect('res_foodinfo', ['food_id', 'food_name', 'food_pic_url','food_describe','food_price','menu_name'])
            .then(
                result => {
                    foods = result;
                }
            )
        return {
            code: 0,
            msg: "查询成功",
            data: foods,
            count: foods.length,
        }
    },
    addFood: async function(data){
        await dbutil.singleSelect('res_menu','menu_id',`menu_name='${data.menu_name}'`)
            .then(
                res =>{
                    data.menu_id = res.menu_id
                }
            )
        delete data.menu_name;
        await dbutil.add('res_food',data);

        return {
            code:200,
            msg: '添加成功',
        }
    },
    editFood: async function(data){
        await dbutil.singleSelect('res_menu','menu_id',`menu_name='${data.menu_name}'`)
            .then(
                res =>{
                    data.menu_id = res.menu_id
                }
            )
        delete data.menu_name;
        await dbutil.update('res_food',`food_id=${data.food_id}`,data);

        return {
            code:200,
            msg: '修改成功',
        }
    },
    delFood: async function(data){
        await dbutil.del('res_food',`food_id=${data.food_id}`);

        return {
            code:200,
            msg: '删除成功',
        }
    },
    saveImg: async function (data) {
        await dbutil.update('res_food', `food_id = ${data.food_id}`, {
            uimg: data.filesName,
        })
        return {
            code: 200,
            msg: '上传文件成功',
            url: data.filesName,
        }
    }
}